﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Standards;
namespace PH.Data.Standards
{
    public class EBMICommand
    {
        public static EBMICollection Gets(EvaluationQuery sea)
        {
            var ret = new EBMICollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<EBMI> q = db.DataContext.EBMIs;
                if (sea != null)
                {
                    if (sea.GradeCode > 0)
                        q = q.Where(p => p.GradeCode == sea.GradeCode);
                    if (sea.Sex > 0)
                        q = q.Where(p => p.Sex == sea.Sex);
                }
                var n = q.Count();
                if (n > 0)
                {
                    q = q.OrderBy(p => p.GradeCode).ThenBy(p => p.Sex).ThenByDescending(p =>p.Score);
                    if (sea != null && sea.PageSize > 0)
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<EBMIModel, EBMI, EBMICollection>(lst);
                    ret.TotalRecordsCount = n;
                }
            }
            return ret;
        }

        static EBMICollection _list;
        public static EBMICollection List
        {
            get
            {
                if (_list == null || _list.TotalRecordsCount == 0)
                    _list = Gets(null);
                return _list;
            }
        }
    }
}
